<template>
  <el-card>
    <el-input
      v-model="page.input"
      placeholder="请输入内容进行查询"
      :clearable="true"
      style="width: 30%; float: left; margin-bottom: 10px"
    ></el-input>

    <el-button
      type="primary"
      @click="loadBona()"
      style="width: 5%; float: left; margin-left: 10px"
      ><i class="el-icon-search"></i
    ></el-button>
    <el-button
      type="success"
      style="width: 8%; float: right; margin-right: 20px"
      @click="beforeAdd()"
      >添加</el-button
    >
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="userName" label="用户" width="180">
      </el-table-column>
      <el-table-column prop="userIdentyid" label="用户身份证Id" width="180">
      </el-table-column>
      <el-table-column prop="bonaGrade" label="信誉积分"> </el-table-column>
      <el-table-column prop="bonaLevel" label="信誉等级"> </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope.row.bonaId)"
            >编辑</el-button
          >
          <el-button
            type="warning"
            style="width: 70px"
            @click="handleDel(scope.row.bonaId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.currentPage"
      :page-sizes="pageInfo.pageSizes"
      :page-size="page.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
    >
    </el-pagination>

    <!-- ------------------------信誉添加模态框---------------------------- -->
    <el-dialog title="修改" :visible.sync="dialog.addBonaShow" width="40%">
      <el-form
        ref="form"
        :model="adddateBonaForm"
        label-width="100px"
        size="medium"
      >
        <el-form-item label="用户姓名">
          <el-select
            v-model="adddateBonaForm.userName"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in users"
              :key="item.userId"
              :label="item.userName"
              :value="item.userName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户身份证号">
          <el-input v-model="adddateBonaForm.userIdentyid"></el-input>
        </el-form-item>
        <el-form-item label="信誉积分">
          <el-input v-model="adddateBonaForm.bonaGrade"></el-input>
        </el-form-item>
        <el-form-item label="信誉等级">
          <el-input v-model="adddateBonaForm.bonaLevel"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.addBonaShow = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- ------------------------信誉模态框---------------------------- -->
    <el-dialog title="修改" :visible.sync="dialog.upBonaShow" width="40%">
      <el-form
        ref="form"
        :model="updateBonaForm"
        label-width="100px"
        size="medium"
      >
        <el-form-item label="信誉积分" prop="bonaGrade">
          <el-input v-model="updateBonaForm.bonaGrade"></el-input>
        </el-form-item>
        <el-form-item label="信誉等级" prop="bonaLevel">
          <el-input v-model="updateBonaForm.bonaLevel"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.upBonaShow = false">取 消</el-button>
        <el-button type="primary" @click="updateBona()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- ------------------------删除模态框---------------------------- -->
    <el-dialog title="修改" :visible.sync="dialog.deleteShow" width="40%">
      <span>确定要删除吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.deleteShow = false">取 消</el-button>
        <el-button type="primary" @click="deleteBona()">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      id: "",
      dialog: {
        upBonaShow: false,
        deleteShow: false,
        addBonaShow: false,
      },
      tableData: {},
      page: {
        currentPage: 1,
        pagesize: 5,
        input: "",
      },
      pageInfo: {
        total: 0,
        pageSizes: [5, 20, 30, 50, 100],
      },
      updateBonaForm: {},
      adddateBonaForm: {},
      users: {},
    };
  },
  methods: {
    add() {
      this.$axios.post("/Bona", this.adddateBonaForm).then((res) => {
        if (res.data.code == 200) {
          this.$message.success("用户信誉添加成功");
        }
      });
      this.dialog.addBonaShow = false;

      this.loadBona();
    },
    beforeAdd() {
      this.dialog.addBonaShow = true;
      this.$axios.get("/users/Bona").then((res) => {
        this.users = res.data.data.users;
      });
    },
    updateBona() {
      this.$axios.put("/Bona", this.updateBonaForm).then((res) => {
        if (res.data.code == 200) {
          this.$message.success("信誉修改成功");
        }
      });
      this.dialog.upBonaShow = false;

      this.loadBona();
    },
    handleEdit(id) {
      this.dialog.upBonaShow = true;
      this.$axios.get("/Bona/" + id).then((res) => {
        this.updateBonaForm = res.data.data.bona;
      });
    },
    handleDel(id) {
      this.dialog.deleteShow = true;
      this.id = id;
    },
    deleteBona() {
      this.dialog.deleteShow = false;
      this.$axios.delete("/Bona/" + this.id).then((res) => {
        this.$message.success("删除成功");
      });

      this.loadBona();
    },
    //当每页显示条数改变时
    handleSizeChange(size) {
      this.page.pagesize = size;
      this.loadBona();
    },
    //当页数改变时
    handleCurrentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.loadBona();
    },
    loadBona() {
      this.$axios.post("/Bona/page", this.page).then((res) => {
        this.tableData = res.data.data.list;
        this.pageInfo.total = res.data.data.pageInfo;
      });
    },
  },
  mounted() {
    this.loadBona();
  },
};
</script>